﻿// <copyright file="AddPaymentToChild.xaml.cs" company="company"> 
// (C) 2010 . All rights reserved
// </copyright>
// <author>Lukasz Bartczak</author>

namespace SecretaryAppNav.Views
{
    using System;
    using System.ServiceModel;
    using System.Windows.Controls;
    using SecretaryAppNav.ClientWcfService;

    /// <summary>
    /// AddPaymentToChild partial class
    /// </summary>
    public partial class AddPaymentToChild : Page
    {
        /// <summary>
        /// Service proxy
        /// </summary>
        private Service1Client client = new Service1Client();

        /// <summary>
        /// Initializes a new instance of the <see cref="AddPaymentToChild"/> class.
        /// </summary>
        public AddPaymentToChild()
        {
            InitializeComponent();
            this.client.GetChildAndOpiekunByFirstnameLastnameCompleted += new EventHandler<GetChildAndOpiekunByFirstnameLastnameCompletedEventArgs>(this.Client_GetChildAndOpiekunByFirstnameLastnameCompleted);
            this.client.AddPaymentToChildCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(this.Client_AddPaymentToChildCompleted);
            this.client.GetMonthsCompleted += new EventHandler<GetMonthsCompletedEventArgs>(this.Client_GetMonthsCompleted);
            this.client.GetMonthsAsync();
            this.client.GetClassKindCompleted += new EventHandler<GetClassKindCompletedEventArgs>(this.Client_GetClassKindCompleted);
            this.client.GetClassKindAsync();
        }

        /// <summary>
        /// Handles the GetClassKindCompleted event of the Client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SecretaryAppNav.ClientWcfService.GetClassKindCompletedEventArgs"/> instance containing the event data.</param>
        private void Client_GetClassKindCompleted(object sender, GetClassKindCompletedEventArgs e)
        {
            this.cbClassKind.DataContext = e.Result;
        }

        /// <summary>
        /// Handles the GetMonthsCompleted event of the Client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SecretaryAppNav.ClientWcfService.GetMonthsCompletedEventArgs"/> instance containing the event data.</param>
        private void Client_GetMonthsCompleted(object sender, GetMonthsCompletedEventArgs e)
        {
            this.cbMonths.DataContext = e.Result;
        }

        /// <summary>
        /// Handles the AddPaymentToChildCompleted event of the Client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.AsyncCompletedEventArgs"/> instance containing the event data.</param>
        private void Client_AddPaymentToChildCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
        }

        /// <summary>
        /// Handles the GetChildAndOpiekunByFirstnameLastnameCompleted event of the Client control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SecretaryAppNav.ClientWcfService.GetChildAndOpiekunByFirstnameLastnameCompletedEventArgs"/> instance containing the event data.</param>
        private void Client_GetChildAndOpiekunByFirstnameLastnameCompleted(object sender, GetChildAndOpiekunByFirstnameLastnameCompletedEventArgs e)
        {
            try
            {
                this.gridViewDataControl1.ItemsSource = e.Result;
            }
            catch (FaultException fe)
            {
                throw new FaultException(fe.Reason, fe.Code, "Exception in Client_GetChildAndOpiekunByFirstnameLastnameCompleted");
            }
            catch (CommunicationException cex)
            {
                throw new CommunicationException("Exception in Client_GetChildAndOpiekunByFirstnameLastnameCompleted " + cex.Message, cex.InnerException);
            }
            catch (Exception exe)
            {
                throw new Exception("Exception in Client_GetChildAndOpiekunByFirstnameLastnameCompleted " + exe.Message, exe.InnerException);
            }
        }

        /// <summary>
        /// Handles the Click event of the BtDodajPlatnosc control - add payment to child.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void BtDodajPlatnosc_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            DzieckoAndOpiekun selectedRow = (DzieckoAndOpiekun)this.gridViewDataControl1.SelectedItems[0];
            this.client.AddPaymentToChildAsync(selectedRow.dzieckoImie, selectedRow.dzieckoNazwisko, selectedRow.opiekunImie, selectedRow.opiekunNazwisko, this.cbClassKind.SelectedValue.ToString(), this.cbMonths.SelectedValue.ToString(), this.radDatePicker1.SelectedDate.Value);
            this.result.Content = "Dodano opłatę do dziecka!";
        }

        /// <summary>
        /// Handles the Click event of the BtSzukajDz control - find child.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void BtSzukajDz_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                this.tbDzImie.Value = (this.tbDzImie.Value == null) ? String.Empty : this.tbDzImie.Value.ToString();
                this.tbDzNazwisko.Value = (this.tbDzNazwisko.Value == null) ? String.Empty : this.tbDzNazwisko.Value.ToString();
                this.client.GetChildAndOpiekunByFirstnameLastnameAsync(this.tbDzImie.Value.ToString(), this.tbDzNazwisko.Value.ToString());
            }
            catch (FaultException fe)
            {
                throw new FaultException(fe.Reason, fe.Code, "Button_click");
            }
            catch (CommunicationException cex)
            {
                throw new CommunicationException("Exception in btSzukajDz_Click " + cex.Message, cex.InnerException);
            }
            catch (Exception exe)
            {
                throw new Exception("Exception in btSzukajDz_Click " + exe.Message, exe.InnerException);
            }
        }
    }
}
